package com.jwinf.cdLibary.controller;

import com.jwinf.cdLibary.domain.Record;
import com.jwinf.cdLibary.service.CDService;
import com.jwinf.cdLibary.tool.WindowUtils;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;


/**
 * @author jwinf
 *
 * 查看历史记录窗口控制类
 */
public class HistoryFrameController implements Initializable {
    @FXML
    private Label namelbl;
    @FXML
    private TableView table;
    @FXML
    private Label goback;
    @FXML
    private TextField nametxt;
    @FXML
    private Label searchIcon;

    private ObservableList<Record> list;
    private CDService cdService = new CDService();

    // 初始化方法
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        componentInit();           // 组件初始化
        addListener();             // 添加监听器
    }

    // 组件初始化
    private void componentInit(){

        // 给标签设置icon样式
        Image image;
        image = new Image(getClass().getResourceAsStream("/icon/searchUserIcon.png"), 32, 32, false, false);
        searchIcon.setGraphic(new ImageView(image));

        image = new Image(getClass().getResourceAsStream("/icon/backIcon.png"), 32, 32, false, false);
        goback.setGraphic(new ImageView(image));

        image = new Image(getClass().getResourceAsStream("/icon/currentUser.png"), 32, 32, false, false);
        namelbl.setGraphic(new ImageView(image));

        // 新建表格的列 并给列绑定光盘的属性
        TableColumn nameCol = new TableColumn("用户名");
        nameCol.setCellValueFactory(new PropertyValueFactory<>("username"));
        nameCol.setPrefWidth(100);
        TableColumn codeCol = new TableColumn("光盘编号");
        codeCol.setCellValueFactory(new PropertyValueFactory<>("code"));
        codeCol.setPrefWidth(100);
        TableColumn cdNameCol = new TableColumn("光盘名");
        cdNameCol.setCellValueFactory(new PropertyValueFactory<>("cdname"));
        cdNameCol.setPrefWidth(100);
        TableColumn stateCol = new TableColumn("状态");
        stateCol.setCellValueFactory(new PropertyValueFactory<>("action"));
        stateCol.setPrefWidth(50);
        TableColumn timeCol = new TableColumn("时间");
        timeCol.setCellValueFactory(new PropertyValueFactory<>("time"));
        timeCol.setPrefWidth(150);

        // 给表格添加列
        table.getColumns().addAll(nameCol, codeCol, cdNameCol, stateCol, timeCol);

    }

    // 添加监听器
    private void addListener(){
        // 当鼠标进入某些组件时改变其样式为手型
        WindowUtils.changeMouse(goback);
        WindowUtils.changeMouse(searchIcon);
        // 在执行某些耗时操作时把鼠标样式等待的圈
        WindowUtils.MouseWait(searchIcon);
    }

    // 查询操作
    @FXML
    private void search(){
        // 获取组件中的数据
        String userName = nametxt.getText();
        // 判断数据合法性并弹出弹窗提示用户
        if(userName == null || userName.isEmpty()){
            WindowUtils.showTip("请输入用户名", "/icon/warnIcon.png", " -fx-background-color:  linear-gradient(to top, #bdc2e8 0%, #bdc2e8 1%, #e6dee9 100%);");
            return;
        }
        // 调用服务层的查询历史记录的方法 并将结果转换成表格支持的数据类型
        list = FXCollections.observableArrayList(cdService.history(userName));
        // 给表格设置数据
        if(!list.isEmpty())
            table.setItems(list);
        else {
            WindowUtils.showTip("没有此用户的相关记录", "/icon/warnIcon.png", "-fx-background-color:  linear-gradient(to top, #bdc2e8 0%, #bdc2e8 1%, #e6dee9 100%);");
            table.setItems(null);
        }
    }

    // 返回管理员主界面方法
    @FXML
    private void change() throws IOException {
        WindowUtils.showFrame("/com/jwinf/cdLibary/view/AdminMainFrame.fxml", "/icon/loginIcon.png", "管理员主界面");
        WindowUtils.destroyStage(goback);
    }
}
